package com.umeox.watch.alphabetlearning

import android.content.ContentResolver
import android.content.Context
import android.media.AudioManager
import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.watch.alphabetlearning.adapter.ViewPagerAdapter
import com.umeox.watch.alphabetlearning.bean.AlphabetBean
import com.umeox.watch.alphabetlearning.widget.VolumeSettingDialog

/**
 * Created by Rambo 2023/7/7
 * Describe:
 */
class LearningActivity : ThemeAppCompatActivity() {

    private lateinit var mViewPager: ViewPager2
    private lateinit var mVolumeIv: ImageView
    private lateinit var mLeftIv: ImageView
    private lateinit var mRightIv: ImageView
    private lateinit var mReplayFl: FrameLayout

    private lateinit var keyArray: Array<String>
    private lateinit var wordArray: Array<String>
    private lateinit var resIdArray: IntArray
    private lateinit var soundResIdArray: IntArray

    private var volumeSettingDialog: VolumeSettingDialog? = null

    private lateinit var alphabetList: MutableList<AlphabetBean>

    private var mCurrentItemIndex: Int? = 0

    private var player: MediaPlayer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_learning)
        mCurrentItemIndex = intent?.getIntExtra("current_index", 1)
        initData()
        initView()
    }

    private fun initView() {
        mViewPager = findViewById(R.id.vp)
        mVolumeIv = findViewById(R.id.iv_volume)
        mLeftIv = findViewById(R.id.iv_left)
        mRightIv = findViewById(R.id.iv_right)
        mReplayFl = findViewById(R.id.replay_fl)
        mReplayFl.visibility = View.GONE

        mViewPager.adapter = ViewPagerAdapter(alphabetList)
        mViewPager.setCurrentItem(mCurrentItemIndex!!, false)
        mViewPager.registerOnPageChangeCallback(ViewPageChange())
        mLeftIv.setOnClickListener {
            mViewPager.currentItem = mCurrentItemIndex!! - 1
        }

        mRightIv.setOnClickListener {
            mViewPager.currentItem = mCurrentItemIndex!! + 1
        }
        mReplayFl.setOnClickListener {
            startPlay(mCurrentItemIndex!!)
        }

        mVolumeIv.setOnClickListener {
            if (volumeSettingDialog == null) {
                volumeSettingDialog = VolumeSettingDialog(this)
            }
            volumeSettingDialog?.show()
        }

        startPlay(mCurrentItemIndex!!)
    }

    private fun initData() {
        keyArray = resources.getStringArray(R.array.alphabet_key_arr)
        wordArray = resources.getStringArray(R.array.alphabet_word_arr)
        resIdArray = IntArray(28)
        soundResIdArray = IntArray(28)
        alphabetList = mutableListOf()
        val resIds = resources.getStringArray(R.array.alphabet_bg_arr)
        for (i in resIds.indices) {
            resIdArray[i] = resources.getIdentifier(resIds[i], "drawable", packageName)
        }
        val soundResIds = resources.getStringArray(R.array.alphabet_sound_arr)
        for (i in soundResIds.indices) {
            soundResIdArray[i] = resources.getIdentifier(soundResIds[i], "raw", packageName)
        }
        for (i in wordArray.indices) {
            val alphabetBean = AlphabetBean(keyArray[i], wordArray[i], resIdArray[i])
            alphabetList.add(alphabetBean)

        }
    }


    inner class ViewPageChange : ViewPager2.OnPageChangeCallback() {

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            mCurrentItemIndex = position
            startPlay(mCurrentItemIndex!!)
            when (position) {
                0 -> {
                    mLeftIv.visibility = View.GONE
                }
                (alphabetList.size - 1) -> {
                    mRightIv.visibility = View.GONE
                }
                else -> {
                    mLeftIv.visibility = View.VISIBLE
                    mRightIv.visibility = View.VISIBLE
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        stopPlay()
    }


    private fun stopPlay() {
        if (player != null && player?.isPlaying!!) {
            player?.stop()
            player?.reset()
            player?.release()
            player = null
        }
    }


    /**
     * 开始响铃
     */
    private fun startPlay(index: Int) {
        try {
            stopPlay()
            player = MediaPlayer.create(this, soundResIdArray[index])
            player?.setAudioStreamType(AudioManager.STREAM_MUSIC)
            player?.isLooping = false
            player?.setOnCompletionListener {
                mReplayFl.visibility = View.VISIBLE
            }
            player?.start()
            mReplayFl.visibility = View.GONE
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 得到资源文件中图片的Uri
     *
     * @param context 上下文对象
     * @param id      资源id
     * @return Uri
     */
    private fun getUriFromDrawableRes(context: Context, id: Int): Uri {
        val resources = context.resources
        val path = (ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + resources.getResourcePackageName(id) + "/"
                + resources.getResourceTypeName(id) + "/"
                + resources.getResourceEntryName(id))
        return Uri.parse(path)
    }


}